package com.mls.fivedistrict.controller.backend;

import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.SysSetting;
import com.mls.fivedistrict.server.SysSettingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * Created by chenxiuxiang on 2018/3/15.
 */
@RestController
@RequestMapping("/api/manager")
public class SysSettingController extends BaseController {

    @Resource(name = "sysSettingService")
    SysSettingService sysSettingService;

    @RequestMapping("/v1/sysSettingList")
    public ServiceResponse getList() {
        return sysSettingService.getSysSettingList();
    }
    @RequestMapping("/v1/sysSettings")
    public ServiceResponse getAll() {
        return sysSettingService.getSysSettings();
    }

    @PostMapping("/v1/sysSetting")
    public ServiceResponse add(@RequestBody SysSetting sysSetting) {
        Optional.ofNullable(sysSetting.getSettingValue()).orElseThrow(() -> new CustomerException("设置值未添加"));
        Optional.ofNullable(sysSetting.getSettingName()).orElseThrow(() -> new CustomerException("设置名称未添加"));
        Date now=new Date();
        sysSetting.setCreateDate(now);
        sysSetting.setUpdateDate(now);
        return sysSettingService.addSysSetting(sysSetting);
    }

    @PutMapping("/v1/sysSetting/{sysSettingId}")
    public ServiceResponse update(@RequestBody SysSetting sysSetting, @PathVariable("sysSettingId") Long id) {
        sysSetting.setId(id);
        sysSetting.setUpdateDate(new Date());
        return sysSettingService.editSysSetting(sysSetting);
    }

    @GetMapping("/v1/sysSetting/{sysSettingId}")
    public ServiceResponse getSetting(@PathVariable("sysSettingId") Long id) {
        return sysSettingService.getSysSetting(id);
    }
    @GetMapping("/v1/sysSettings")
    public ServiceResponse getSettingList(){
        return sysSettingService.getSysSettings();
    }
    @PutMapping("/v1/sysSettings")
    public ServiceResponse updateSettings(@RequestBody Map<String,String> params){
        return sysSettingService.updateSettings(params);
    }



}
